package com.chenfubing.ps.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfubing.ps.entity.Access;
import com.chenfubing.ps.entity.pojo.RequestPojo;
import com.chenfubing.ps.entity.result.JsonResult;
import com.chenfubing.ps.service.IAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenfubing
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/access")
public class AccessController {

    @Autowired
    private IAccessService iAccessService;

    /**
     * 添加门禁
     *
     * @param access
     */
    @PostMapping("/addAccess")
    public JsonResult addAccess(@Validated @RequestBody Access access) {
        iAccessService.addAccess(access);
        return new JsonResult();
    }

    /**
     * 删除门禁
     *
     * @param id
     */
    @GetMapping("/deleteAccess")
    public JsonResult deleteAccess(@RequestParam int id) {
        iAccessService.deleteAccess(id);
        return new JsonResult();
    }

    /**
     * 查询
     * @param requestPojo
     * @return
     */
    @PostMapping("/findAccess")
    public JsonResult findAccess(@RequestBody RequestPojo requestPojo) {
        return new JsonResult<>(iAccessService.findAccess(requestPojo.getStartTime(), requestPojo.getEndTime(), requestPojo.getLicense()));
    }

    /**
     * 获取门禁分页
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getAccessPage")
    public JsonResult getAccessPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return new JsonResult(iAccessService.getAccessPage(pageNum, pageSize));
    }

    /**
     * 导出门禁表
     */
    @GetMapping("/exportAccess")
    public void export(){
        iAccessService.export();
    }

    @GetMapping("/getOneAccess")
    public JsonResult getOneAccess(@RequestParam int id){
        return new JsonResult(iAccessService.getBaseMapper().selectById(id));
    }

    @GetMapping("/getUserByAccessId")
    public JsonResult getUserByAccessId(@RequestParam int id){
        return new JsonResult(iAccessService.getUserByAccessId(id));
    }

}
